package lab.dexter.bbsreader;

import lab.dexter.bbsreader.managers.BoardPostsManager;
import lab.dexter.bbsreader.managers.ClassifiedListManager;
import lab.dexter.bbsreader.managers.FavoriteListManager;
import lab.dexter.bbsreader.managers.FocusTopicsManager;
import lab.dexter.bbsreader.managers.PostManager;
import lab.dexter.bbsreader.managers.SectionFocusManager;
import lab.dexter.bbsreader.managers.TestManager;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class ListBrowser extends Activity {
	public static final String MGR = "ListManager";
	public static final int MGR_TEST = 0;
	public static final int MGR_SECTION_FOCUS = 1;
	public static final int MGR_FOCUS_TOPICS = 2;
	public static final int MGR_BOARD_POSTS = 3;
	public static final int MGR_POST = 4;
	public static final int MGR_CLASSFIED_LIST = 5;
	public static final int MGR_FAVORITE_LIST = 6;
	
    private ListManager mManager;
    private String original_title;
    
    public class MyTask extends AsyncTask<ListManager, Void, Integer> {

		@Override
		protected void onPostExecute(Integer result) {
			// TODO Auto-generated method stub
			mManager.notifyDataChanged();
			super.onPostExecute(result);
			ListBrowser.this.setProgressBarIndeterminateVisibility(false);
		}

		@Override
		protected Integer doInBackground(ListManager... params) {
			// real work is done in actual implementation of ListManager
			return params[0].update();
		}

		@Override
		protected void onCancelled() {
			// TODO Auto-generated method stub
			super.onCancelled();
		}

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
		}

		@Override
		protected void onProgressUpdate(Void... values) {
			// TODO Auto-generated method stub
			super.onProgressUpdate(values);
		}
    	
    }
    


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

        // extract target information from intent
        // create corresponding list manager and list items.
        // list manager and list items should hide all details
        Intent intent = this.getIntent();
        

        int mgr_code = intent.getIntExtra(MGR, 0);
        //Log.v("CODE", mgr_code + "");
        
        switch(mgr_code) {
        case MGR_SECTION_FOCUS:
        	mManager = new SectionFocusManager(this, intent);
        	break;
        case MGR_FOCUS_TOPICS:
        	mManager = new FocusTopicsManager(this, intent);
        	break;
        case MGR_BOARD_POSTS:
        	mManager = new BoardPostsManager(this, intent);
        	break;
        case MGR_POST:
        	mManager = new PostManager(this, intent);
        	break;
        case MGR_CLASSFIED_LIST:
        	mManager = new ClassifiedListManager(this, intent);
        	break;
        case MGR_FAVORITE_LIST:
        	mManager = new FavoriteListManager(this, intent);
        	break;
        case MGR_TEST:
        	mManager = new TestManager(this);
        	break;
        }

        original_title = (String) this.getTitle();
    	this.setTitle(original_title + " " + mManager.getTitleSuffix());
    	
        this.setContentView(R.layout.main);
        ListView lv = (ListView) this.findViewById(R.id.main_list);

    	lv.setAdapter(mManager);


        this.registerForContextMenu(lv);
        lv.invalidate();

        lv.setOnItemClickListener(new OnItemClickListener() {
          public void onItemClick(AdapterView<?> parent, View view,
              int position, long id) {
            // When clicked, show a toast with the TextView text
            //Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
            //    Toast.LENGTH_SHORT).show();
        	  
        	  // depends on the position of the click. if it's the last 
        	  // item in the list, then we should trigger a asnyc task
        	  // otherwise just call the onClick function of list manager
        	  if(mManager.onItemClick(position)) {
        		  // return true, means another execution is requested
        		  ListBrowser.this.setProgressBarIndeterminateVisibility(true);
        		  new MyTask().execute(mManager);
        	  }
          }
        });

        
        if(mManager.hasBottomPanel()) {
        	LinearLayout panel = (LinearLayout)this.findViewById(R.id.bottom_panel);
        	LayoutInflater vi = (LayoutInflater)(this.getSystemService(Context.LAYOUT_INFLATER_SERVICE));
			View view = vi.inflate(mManager.getBottomPanelResource(), null);
			int[] members = mManager.getBottomPanelMembers();
			OnClickListener listener = new OnClickListener() {
				public void onClick(View v) {
					Log.v("BottomPanel", v.toString());
					if(mManager.onClickOnBottomPanel(v)) {
						ListBrowser.this.setProgressBarIndeterminateVisibility(true);
						new MyTask().execute(mManager);
					}
				}
			};
			for(int i = 0; i < members.length; i ++) {
				view.findViewById(members[i]).setOnClickListener(listener);
			}
        	panel.addView(view);
        }
		
        this.setProgressBarIndeterminateVisibility(true);
		new MyTask().execute(mManager);
    }


	@Override
	protected void onDestroy() {
		this.setTitle(original_title);
		super.onDestroy();
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		mManager.onContextItemSelected(item);
		return super.onContextItemSelected(item);
	}



	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		if(mManager.hasContextMenu()) {
			MenuInflater inflater = getMenuInflater();
			inflater.inflate(mManager.getContextMenuResource(), menu);
		}
	}
    
    
}